import { createRouter, createWebHashHistory } from 'vue-router'

const routes: Array<any> = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('../views/foreground/LoginPage.vue')
  },
  {
    // 主页
    name: 'home',
    path: '/home',
    component: () => import('../views/foreground/HomePage.vue')
  },
  {
    // 归档
    name: 'archive',
    path: '/archive',
    component: () => import('../views/foreground/ArchivesPage.vue')
  },
  {
    // 分类
    name: 'classifications',
    path: '/classifications',
    component: () => import('../views/foreground/classification/Classifications.vue')
  },
  {
    // 分类详情
    name: 'classificationDetail',
    path: '/classification/detail',
    component: () => import('../views/foreground/classification/ClassificationDetail.vue')
  },
  {
    // 标签页
    name: 'tags',
    path: '/tags',
    component: () => import('../views/foreground/tags/TagsPage.vue')
  },
  {
    // 标签详情页
    name: 'tagDetail',
    path: '/tags/detail',
    component: () => import('../views/foreground/tags/TagsDetailPage.vue')
  },
  {
    // 关于页
    name: 'concerning',
    path: '/concerning',
    component: () => import('../views/foreground/ConcerningPage.vue')
  },
  {
    // 文章页
    name: 'article',
    path: '/article',
    component: () => import('../views/foreground/articales/ArticlesPage.vue')
  },
  {
    // 文章详情页
    name: 'articleDetail',
    path: '/article/detail',
    component: () => import('../views/foreground/articales/ArticalDetail.vue')

  },
  {
    name: 'layout',
    path: '/layout',
    component: () => import('../layout/Layout.vue'),
    redirect: '/layout/portal',
    children: [
      {
        name: 'portal',
        path: 'portal',
        component: () => import('../views/backstage/PortalPage.vue')
      },
      { // 文章管理
        name: 'articles',
        path: 'articles',
        component: () => import('../views/backstage/ArticlesManage.vue'),
      },
      {
        // 新增文章
        name: 'increaseArticle',
        path: 'increaseArticle',
        component: () => import('../views/backstage/IncreaseArticle.vue')
      },
      { // 分类管理
        name: 'classification',
        path: 'classification',
        component: () => import('../views/backstage/Classification.vue')
      },
      { // 标签管理
        name: 'tag',
        path: 'tag',
        component: () => import('../views/backstage/TagPage.vue')
      },
      { // 关于管理
        name: 'about',
        path: 'about',
        component: () => import('../views/backstage/AboutPage.vue')
      },
      { // 用户管理
        name: 'user',
        path: 'user',
        component: () => import('../views/backstage/UserPage.vue')
      },
      { // 评论管理
        name: 'review',
        path: 'review',
        component: () => import('../views/backstage/ReviewPage.vue')
      },
      { // 评论管理
        name: 'homeConfiguration',
        path: 'homeConfiguration',
        component: () => import('../views/backstage/HomeConfiguration.vue')
      },
    ]
  }

]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 前置路由导航守卫
router.beforeEach((to, from) => {
  const token = localStorage.getItem('token')
  if (!token && to.name !== 'login') {
    // 将用户重定向到登录页面
    return 'login'
  }
})

export default router